<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>读者详情</title>
    <link rel="stylesheet" type="text/css" href="/site/css/styles.css">
</head>
<body>
<div id="app">
    <app-header></app-header>
    <app-sidebar></app-sidebar>

    <div class="main-content">
        <h2>读者详情</h2>
        <div>
            <p><strong>ID:</strong> {{ reader.readerId }}</p>
            <p><strong>名字:</strong> {{ reader.name }}</p>
            <p><strong>性别:</strong> {{ reader.gender }}</p>
            <p><strong>联系电话:</strong> {{ reader.phone }}</p>
            <p><strong>电子邮箱:</strong> {{ reader.email }}</p>
            <a href="/reader/list" class="back-button">返回读者列表</a>
        </div>
    </div>
</div>
<!-- 引入 Vue -->
<script src="/assets/vue/vue3.min.js"></script>
<!-- 引入 Axios -->
<script src="/assets/axios/axios.min.js"></script>
<!-- 引入组件 -->
<script src="/site/js/component/header.js"></script>
<script src="/site/js/component/sidebar.js"></script>

<script>
    const {reactive, createApp, onMounted} = Vue;

    const app = {
        // 使用局部注册的方式注册组件
        components: {
            AppHeader,
            AppSidebar
        },
        setup() {
            // 定义响应式数据 reader
            const reader = ref({});
            // 获取读者详情的方法
            const fetchreaderById = () => {
                const urlParams = new URLSearchParams(window.location.search);
                const id = urlParams.get('id');
                if (id) {
                    axios.get(`/api/reader/${id}`)
                        .then(response => {
                            if (response.data.code === 200) {
                                reader.value = response.data.data;
                            } else {
                                console.error('获取读者详情失败:', response.data.message || '未知原因');
                                alert('获取读者详情失败，请稍后再试');
                            }
                        })
                        .catch(error => {
                            console.error('获取读者详情失败:', error);
                            alert('获取读者详情失败，请稍后再试');
                        });
                }
            };

            // 在组件挂载时调用 fetchreaderById
            onMounted(() => {
                fetchreaderById();
            });

            // 返回数据和方法给模板使用
            return {reader};
        }
    };
    createApp(app).mount('#app');
</script>
</body>
</html>